package com.sddz.pacs.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageInfo;
import com.sddz.pacs.annotation.SysLogger;
import com.sddz.pacs.config.FtpConfig;
import com.sddz.pacs.dto.JsonResult;
import com.sddz.pacs.dto.PageModelParams;
import com.sddz.pacs.entity.HealthUser;
import com.sddz.pacs.entity.SysUser;
import com.sddz.pacs.entity.UserResult;
import com.sddz.pacs.service.HealthUserService;
import com.sddz.pacs.service.SysStationService;
import com.sddz.pacs.service.SysUserService;
import com.sddz.pacs.service.UserResultService;
import com.sddz.pacs.support.Constant;
import com.sddz.pacs.util.FtpUtil;
import com.sddz.pacs.util.ImageUtil;
import com.sddz.pacs.util.ShiroUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author haoshili
 * @Date 2018/9/12
 */
@Controller
@RequestMapping("/healthUser")
@Api(description = "增加人员信息接口")
public class HealthUserController {

    @Autowired
    private HealthUserService healthUserService;

    @Autowired
    private UserResultService userResultService;

    @Autowired
    private FtpConfig ftpConfig;

    @PostMapping(value = "/addHealthUser")
    @ResponseBody
    //@SysLogger(value = "增加居民信息",type = "增加居民信息日志管理")
    @ApiOperation(value="增加居民信息", notes="居民信息数据")
    @ApiImplicitParam(name = "addHealthUser",value = "居民用户json对象",required = true)
        public JsonResult addHealthUser(@RequestBody JSONObject jsonObject) throws Exception {
        HealthUser healthUser= JSON.parseObject(jsonObject.toJSONString(),HealthUser.class);
        UserResult userResult= JSON.parseObject(jsonObject.toJSONString(),UserResult.class);
        //1.获取参数
//        String name = jsonObject.getString("name");//居民姓名
//        Integer age = jsonObject.getInteger("age");//居民年龄
//        String gender = jsonObject.getString("gender");//居民性别
//        String idType = jsonObject.getString("idType");//居民卡类型
//        String idCard = jsonObject.getString("idCard");//居民卡号
//        String tel = jsonObject.getString("tel");//居民电话
//        String homeAddr = jsonObject.getString("homeAddr");//居民地址
//        String stationId = jsonObject.getString("stationId");//服务站id

//        String createDate = jsonObject.getString("createDate");//居民创建时间目前系统时间

//        String checkName = jsonObject.getString("checkName");//报告医师
//        String remark = jsonObject.getString("remark");//检查报告
//        String fileNumber = jsonObject.getString("fileNumber");//档案编号
//        String checkDate = jsonObject.getString("checkDate");//检查时间
//        String baddr1 = jsonObject.getString("baddr1");//B1超影像
//        String baddr2 = jsonObject.getString("baddr2");//B2超影像




//        HealthUser healthUser=new HealthUser();

//        EntityWrapper<HealthUser> ew = new EntityWrapper<>();
//        ew.eq("ID_CARD", idCard);
//        HealthUser healthUser= healthUserService.selectOne(ew);
//        boolean result=false;
//        if(healthUser ==null)
//        {
//            healthUser=new HealthUser();
//        }
//        healthUser.setName(name);
//        healthUser.setAge(age);
//        healthUser.setGender(gender);
//        healthUser.setIdType(idType);
//        healthUser.setIdCard(idCard);
//        healthUser.setTel(tel);
//        healthUser.setHomeAddr(homeAddr);
//        healthUser.setStationId(stationId);
//        healthUser.setCreateDate(new Date());
//
//        if(healthUser!=null){
//            result=healthUserService.update(healthUser,ew);
//            if(result==false){
//                return JsonResult.failure("居民个人信息更新失败");
//            }
//        }else{
//            healthUserService.insert(healthUser);
//        }
        //
        boolean result =  healthUserService.insertOrUpdateUser(healthUser);
        if(result==false){
            return JsonResult.failure("居民个人信息保存失败！");
        }


        //3.处理图片存储
        String savePath = Constant.IDENTITY_FILE_SAVE_PATH + healthUser.getIdCard()+"/"+userResult.getCheckDate()+"/";
        boolean b1 = FtpUtil.uploadFile(ftpConfig.getAddress(), ftpConfig.getPort(), ftpConfig.getUserName(), ftpConfig.getPassWord(),
                Constant.IDENTITY_FILE_SAVE_PATH , healthUser.getIdCard()+"/"+userResult.getCheckDate()+"/", "b1.png", ImageUtil.base64ToStream(userResult.getbAddr1()));

        boolean b2 = FtpUtil.uploadFile(ftpConfig.getAddress(), ftpConfig.getPort(), ftpConfig.getUserName(), ftpConfig.getPassWord(),
                Constant.IDENTITY_FILE_SAVE_PATH , healthUser.getIdCard()+"/"+userResult.getCheckDate()+"/", "b2.png", ImageUtil.base64ToStream(userResult.getbAddr2()));

        if(!(b1&&b2)){
            return JsonResult.failure("图片存储失败");
        }

        userResult.setbAddr1(savePath);
        userResult.setbAddr2(savePath);
        boolean flag= userResultService.insert(userResult);
        return flag ? JsonResult.success("保存成功") : JsonResult.failure("保存失败");
    }
}
